package com.quanquan.management.service;

import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.GroupBuyingDTO;
import com.quanquan.management.dto.ShoppingKillDTO;
import com.quanquan.management.entity.SysUser;
import com.quanquan.management.request.GroupBuyingRequestBody;
import com.quanquan.management.request.ShoppingKillRequestBody;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import utils.Request;

@Service
public class GroupBuyingService {


    @Autowired
    private SystemLogService systemLogService;

    @Value("${module-url.storeUrl}")
    private String storeUrl;


    public boolean addShoppingKill(GroupBuyingRequestBody requestBody) {

        if (null == requestBody) {
            return false;
        }
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        requestBody.setSysUserId(sysUser.getId());

//        String remark = "添加秒杀商品信息";
//        systemLogService.sendSystemLog( sysUser,remark);

        String json = Request.postJson(storeUrl + "group_buy/add", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {

            return true;

        }
        return false;
    }


    public GroupBuyingDTO findById(String id) {
        GroupBuyingRequestBody requestBody = new GroupBuyingRequestBody();
        requestBody.setGroupBuyingId(id);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        requestBody.setSysUserId(sysUser.getId());
//
//        String remark = "查询秒杀商品详细信息";
//        systemLogService.sendSystemLog( sysUser,remark);

        String json = Request.postJson(storeUrl + "group_buy/edit_management", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        GroupBuyingDTO groupBuyingDTO = null;
        if (0 == resCode) {
            JSONObject data = jsonObject.getJSONObject("data");
            groupBuyingDTO = data.toJavaObject(GroupBuyingDTO.class);
        }
        return groupBuyingDTO;
    }

    public boolean update(GroupBuyingRequestBody requestBody) {
        if (null == requestBody) {
            return false;
        }
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        requestBody.setSysUserId(sysUser.getId());

//        String remark = "更新秒杀商品信息";
//        systemLogService.sendSystemLog( sysUser,remark);

        String json = Request.postJson(storeUrl + "group_buy/update", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }
}
